﻿using LeHouse.Server.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeHouse.Server.Service
{
    public class transmitHelper
    {
        /// <summary>
        /// 根据报告号获取所有的相关信息
        /// </summary>
        /// <returns></returns>
        public static TrackRecords transmitHelperByReportNumber(string T_ReportNumber)
        {
            TrackRecords obj = new TrackRecords();
            Collateral_PersonalService personalservice = new Collateral_PersonalService();//创建抵押物权益状况表

            var d1 = personalservice.Select().Where(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
            if(d1==null)
            {
                return null;
            }
            obj.T_ReportNumber = T_ReportNumber;

            Collateral_BorrowerService borrowerservice = new Collateral_BorrowerService();//借款人信息表
            Collateral_ZoneService zoneservice=new Collateral_ZoneService();//创建抵押物区位状况表

            var d2 = borrowerservice.Select().Where(p => p.T_ReportNumber == T_ReportNumber).ToList();
            

            obj.T_State = d1.T_State;//状态
            obj.T_Linkman = d1.T_Linkman;//看房联系人
            obj.T_Phone = d1.T_Phone;//借款人电话
            obj.T_Located = d1.T_Located;//房屋坐落
            obj.T_Date = d1.T_Date.ToString();//登记日期
            obj.T_Loanyear = d1.T_Loanyear;//房屋剩余年限
            obj.T_Builtuparea=d1.T_Builtuparea;//建筑面积
             obj.T_Iid=d1.T_Iid.ToString();//登记员
             obj.T_ValuationName=d1.T_ValuationName.ToString();//-估价师（从员工表中选择）默认为勘察员
             obj.T_ValuationPrice = d1.T_ValuationPrice;//估价单价评估单价(元/㎡) 
             obj.T_Valueofassessment=d1.T_Valueofassessment;//评估总值(元) --'=评估单价*建筑面积,精准到百元位 
             obj.T_ValuationDate = d1.T_ValuationDate.ToString();//估价时点
             obj.T_Mortgageriskrate = d1.T_Mortgageriskrate;//抵押风险率
             obj.T_MaximumMortgage = d1.T_MaximumMortgage;//最高抵押额(元)

             System_StaffService staffService = new System_StaffService();
             var T_Iid = staffService.Select(p => p.T_StaffID == Convert.ToInt32(obj.T_Iid)).FirstOrDefault();//根据id获取登记员姓名
            if(T_Iid!=null)
            {
                obj.T_Iid = T_Iid.T_Name;
            }
            if(!string.IsNullOrEmpty( obj.T_ValuationName))
            {
                var T_ValuationName = staffService.Select(p => p.T_StaffID == Convert.ToInt32(obj.T_ValuationName)).FirstOrDefault();//根据id获取估价师姓名
                if (T_ValuationName != null)
                {
                    obj.T_ValuationName = T_ValuationName.T_Name;
                }
            }
           
             StringBuilder T_Name = new StringBuilder();
             StringBuilder T_Idcard = new StringBuilder();
             foreach (var x in d2)
             {
                 if(d2[0].T_BorrowerID == x.T_BorrowerID)
                 {
                     T_Idcard.Append(x.T_Idcard);
                     T_Name.Append(x.T_Name);
                 }
                 else
                 {
                     T_Idcard.Append(","+x.T_Idcard);
                     T_Name.Append(","+x.T_Name);
                 }
             }
             obj.T_Name = T_Name.ToString();
             obj.T_Idcard = T_Idcard.ToString();

            return obj;
        }


        /// <summary>
        /// 大型传值帮助方法-根据报告号获取所有的相关信息
        /// </summary>
        public static bigByValueHelper bigByValueHelperByReportNumber(string T_ReportNumber)
        {
            bigByValueHelper d1 = new bigByValueHelper()
            {
                T_ReportNumber = T_ReportNumber,
                personal = new Collateral_PersonalService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault()
            };
               d1.ownership = new Collateral_ownershipService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
               d1.borrower = new Collateral_BorrowerService().Select(p => p.T_ReportNumber == T_ReportNumber).ToList();
               d1.community = new Collateral_CommunityService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
               d1.facility = new Collateral_FacilityService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
               d1.indoor = new Collateral_IndoorService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
               d1.zone = new Collateral_ZoneService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
               d1.sitePicture = new Collateral_SitePictureService().Select(p => p.T_ReportNumber == T_ReportNumber).ToList();
               d1.businessContract = new Collateral_BusinessContractService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
               d1.taxcertificate = new Collateral_TaxcertificateService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
               d1.staff = new System_StaffService().Select(p => p.T_StaffID == d1.personal.T_Iid).FirstOrDefault();
               d1.realestate = new Collateral_RealestateService().Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
               
            return d1;
        }


        /// <summary>
        /// 将小写数字转化为大写汉子
        /// </summary>
        /// <param name="Digital"></param>
        /// <returns></returns>
        public static string chang(double Digital)
        {
            //将小写金额转换成大写金额           
            String[] MyScale = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
            String[] MyBase = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            String moneyStr = "";
            bool isPoint = false;
            string moneyDigital = Digital.ToString();
            if (moneyDigital.IndexOf(".") != -1)
            {
                moneyDigital = moneyDigital.Remove(moneyDigital.IndexOf("."), 1);
                isPoint = true;
            }
            for (int i = moneyDigital.Length; i > 0; i--)
            {
                int MyData = Convert.ToInt16(moneyDigital[moneyDigital.Length - i].ToString());
                moneyStr += MyBase[MyData];
                if (isPoint == true)
                {
                    moneyStr += MyScale[i - 1];
                }
                else
                {
                    moneyStr += MyScale[i + 1];
                }
            }
            while (moneyStr.Contains("零零"))
                moneyStr = moneyStr.Replace("零零", "零");
            moneyStr = moneyStr.Replace("零亿", "亿");
            moneyStr = moneyStr.Replace("亿万", "亿");
            moneyStr = moneyStr.Replace("零万", "万");
            moneyStr = moneyStr.Replace("零仟", "零");
            moneyStr = moneyStr.Replace("零佰", "零");
            moneyStr = moneyStr.Replace("零拾", "零");
            while (moneyStr.Contains("零零"))
                moneyStr = moneyStr.Replace("零零", "零");
            moneyStr = moneyStr.Replace("零圆", "圆");
            moneyStr = moneyStr.Replace("零角", "");
            moneyStr = moneyStr.Replace("零分", "");
            moneyStr = moneyStr + "整";
            return moneyStr;
        }



        /// <summary>
        /// 根据字典编号获取字典名称
        /// </summary>
        /// <param name="DictionaryID"></param>
        /// <returns></returns>
        public static string getDictionaryNameByDictionaryID(int DictionaryID)
        {
            System_DictionaryService dictionaryService = new System_DictionaryService();
            var d1 = dictionaryService.Select(p => p.DictionaryID == DictionaryID).FirstOrDefault();
            return d1 != null ? d1.DictionaryName : "";
        }

        /// <summary>
        /// 根据id获取该员工信息
        /// </summary>
        /// <param name="StaffID"></param>
        /// <returns></returns>
        public static System_Staff getStaffByStaffID(int StaffID)
        {
            System_StaffService staffService = new System_StaffService();
            var d1 = staffService.SelectById(StaffID);
            return d1;
        }

        /// <summary>
        /// 根据部门id获取部门信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static System_Department getDepartmentByDid(int id)
        {
            System_DepartmentService system_Department = new System_DepartmentService();
            var d1 = system_Department.SelectById(id);
            return d1;
        }


    }
}
